<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-smtplib.html" />
<link rel="prev" href="module-imaplib.html" />
<link rel="parent" href="internet.html" />
<link rel="next" href="nntp-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>18.12 nntplib -- NNTP protocol client</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.11.2 imap4 Example"
  href="imap4-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.12.1 nntp Objects"
  href="nntp-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="imap4-example.html">18.11.2 IMAP4 Example</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="nntp-objects.html">18.12.1 NNTP Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION00201200000000000000000">
18.12 <tt class="module">nntplib</tt> --
         NNTP protocol client</a>
</h1>

<p>
<a name="module-nntplib"></a>

<p>
<a id='l2h-4165' xml:id='l2h-4165'></a><a id='l2h-4173' xml:id='l2h-4173'></a>

<p>
This module defines the class <tt class="class">NNTP</tt> which implements the client
side of the NNTP protocol.  It can be used to implement a news reader
or poster, or automated news processors.  For more information on NNTP
(Network News Transfer Protocol), see Internet <a class="rfc" id='rfcref-103114' xml:id='rfcref-103114'
href="http://www.faqs.org/rfcs/rfc977.html">RFC 977</a>.

<p>
Here are two small examples of how it can be used.  To list some
statistics about a newsgroup and print the subjects of the last 10
articles:

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; s = NNTP('news.cwi.nl')
&gt;&gt;&gt; resp, count, first, last, name = s.group('comp.lang.python')
&gt;&gt;&gt; print 'Group', name, 'has', count, 'articles, range', first, 'to', last
Group comp.lang.python has 59 articles, range 3742 to 3803
&gt;&gt;&gt; resp, subs = s.xhdr('subject', first + '-' + last)
&gt;&gt;&gt; for id, sub in subs[-10:]: print id, sub
... 
3792 Re: Removing elements from a list while iterating...
3793 Re: Who likes Info files?
3794 Emacs and doc strings
3795 a few questions about the Mac implementation
3796 Re: executable python scripts
3797 Re: executable python scripts
3798 Re: a few questions about the Mac implementation 
3799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
3802 Re: executable python scripts 
3803 Re: \POSIX{} wait and SIGCHLD
&gt;&gt;&gt; s.quit()
'205 news.cwi.nl closing connection.  Goodbye.'
</pre></div>

<p>
To post an article from a file (this assumes that the article has
valid headers):

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; s = NNTP('news.cwi.nl')
&gt;&gt;&gt; f = open('/tmp/article')
&gt;&gt;&gt; s.post(f)
'240 Article posted successfully.'
&gt;&gt;&gt; s.quit()
'205 news.cwi.nl closing connection.  Goodbye.'
</pre></div>

<p>
The module itself defines the following items:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-4166' xml:id='l2h-4166' class="class">NNTP</tt></b>(</nobr></td>
  <td><var>host</var><big>[</big><var>, port
                        </var><big>[</big><var>, user</var><big>[</big><var>, password
			</var><big>[</big><var>, readermode</var><big>]</big><var>
			</var><big>[</big><var>, usenetrc</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a new instance of the <tt class="class">NNTP</tt> class, representing a
connection to the NNTP server running on host <var>host</var>, listening at
port <var>port</var>.  The default <var>port</var> is 119.  If the optional
<var>user</var> and <var>password</var> are provided, 
or if suitable credentials are present in <span class="file">&nbsp;/.netrc</span> and the
optional flag <var>usenetrc</var> is true (the default),
the "<tt class="samp">AUTHINFO USER</tt>" and "<tt class="samp">AUTHINFO PASS</tt>" commands are used to
identify and authenticate the user to the server.  If the optional
flag <var>readermode</var> is true, then a "<tt class="samp">mode reader</tt>" command is
sent before authentication is performed.  Reader mode is sometimes
necessary if you are connecting to an NNTP server on the local machine
and intend to call reader-specific commands, such as "<tt class="samp">group</tt>".  If
you get unexpected <tt class="exception">NNTPPermanentError</tt>s, you might need to set
<var>readermode</var>.  <var>readermode</var> defaults to <code>None</code>.
<var>usenetrc</var> defaults to <code>True</code>.

<p>

<span class="versionnote">Changed in version 2.4:
<var>usenetrc</var> argument added.</span>

</dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4167' xml:id='l2h-4167' class="exception">NNTPError</tt></b></dt>
<dd>
Derived from the standard exception <tt class="exception">Exception</tt>, this is the
base class for all exceptions raised by the <tt class="module">nntplib</tt> module.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4168' xml:id='l2h-4168' class="exception">NNTPReplyError</tt></b></dt>
<dd>
Exception raised when an unexpected reply is received from the
server.  For backwards compatibility, the exception <code>error_reply</code>
is equivalent to this class.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4169' xml:id='l2h-4169' class="exception">NNTPTemporaryError</tt></b></dt>
<dd>
Exception raised when an error code in the range 400-499 is
received.  For backwards compatibility, the exception
<code>error_temp</code> is equivalent to this class.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4170' xml:id='l2h-4170' class="exception">NNTPPermanentError</tt></b></dt>
<dd>
Exception raised when an error code in the range 500-599 is
received.  For backwards compatibility, the exception
<code>error_perm</code> is equivalent to this class.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4171' xml:id='l2h-4171' class="exception">NNTPProtocolError</tt></b></dt>
<dd>
Exception raised when a reply is received from the server that does
not begin with a digit in the range 1-5.  For backwards
compatibility, the exception <code>error_proto</code> is equivalent to this
class.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4172' xml:id='l2h-4172' class="exception">NNTPDataError</tt></b></dt>
<dd>
Exception raised when there is some error in the response data.  For
backwards compatibility, the exception <code>error_data</code> is
equivalent to this class.
</dd></dl>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="nntp-objects.html">18.12.1 NNTP Objects</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.11.2 imap4 Example"
  href="imap4-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.12.1 nntp Objects"
  href="nntp-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="imap4-example.html">18.11.2 IMAP4 Example</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="nntp-objects.html">18.12.1 NNTP Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
